using System.IO;
using System.Reflection;
using System.Resources;
using System.Web.Mvc;
using System.Web.Security;
using C3.Directory.Services;
using CSI.Directory.Core.Model;
using CSI.Directory.Core.Repositories;
using CSI.Directory.Core.Services;
using CSI.Directory.Services;
using StructureMap;

namespace C3.Directory.Web.Filters
{
    public class ManagementFilter : ActionFilterAttribute
    {
        public ISessionService SessionService
        {
            get { return ObjectFactory.GetInstance<ISessionService>(); }

        }

        
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var replace = "<script>parent.location.replace('/Directory/Login/Manage');</script>";
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //if (UserService.CurrentUser == null)
                //    UserService.GetUserByIdentity(filterContext.HttpContext.User.Identity);
                if (SessionService.CurrentUser != null)
                    if (SessionService.CurrentUser.IsInRole(ManagementRoles.Roles.ToArray()))
                        return;
                    
                        
                filterContext.HttpContext.Response.Write(replace);
                //filterContext.HttpContext.Response.StatusCode = 401;
                //filterContext.HttpContext.Response.StatusDescription = "Unauthorized";
                filterContext.HttpContext.Response.End();
            }
            
            filterContext.HttpContext.Response.Write(replace);
            //filterContext.HttpContext.Response.StatusCode = 401;
            //filterContext.HttpContext.Response.StatusDescription = "Unauthorized";
            filterContext.HttpContext.Response.End();
            
        }
    }
}